//
//  YBVipServiceVC.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/6/5.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift

class YBVipServiceVC: BaseVC {
    
    // MARK: Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigation.item.title = "服务"
        view.backgroundColor = kRGBColor(21, 25, 49)
        
        let items: [YBVipServiceItemView.ItemType] = [.p5, .p6, .p7, .p8]
        items.forEach { type in
            let item = YBVipServiceItemView(type: type)
            stackView.addArrangedSubview(item)
        }
        
        let tSize: CGFloat = UIDevice.bk_isiPhone ? 16 : 20
        let dSize: CGFloat = UIDevice.bk_isiPhone ? 12 : 16
        let t1Label = self.bk_addLabel(text: "邀请试用", font: .systemFont(ofSize: tSize, weight: .medium), bgColor: .clear, textColor: .white)
        let t2Label = self.bk_addLabel(text: "超值选购", font: .systemFont(ofSize: tSize, weight: .medium), bgColor: .clear, textColor: .white)
        let d1Label = self.bk_addLabel(text: "全网公测\(UIDevice.bk_isiPhone ? "\n" : " ")免费惊喜礼包", font: .systemFont(ofSize: dSize), bgColor: .clear, textColor: .white)
        let d2Label = self.bk_addLabel(text: "优惠多多\(UIDevice.bk_isiPhone ? "\n" : " ")会员福利分享", font: .systemFont(ofSize: dSize), bgColor: .clear, textColor: .white)
        
        view.addSubview(scrollView)
        scrollView.addSubviews([img1View, des1Label, img2View, img3View, img4View, stackView, img9View, des2Label, personalItemView, companyItemView])
        img3View.addSubviews([t1Label, d1Label])
        img4View.addSubviews([t2Label, d2Label])
        
        scrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalTo(kScreenWidth)
        }
        
        img1View.snp.makeConstraints { make in
            make.top.equalTo(28)
            make.centerX.equalToSuperview()
            make.height.equalTo(35)
            make.width.equalTo(kScreenWidth*0.616)
        }
        
        des1Label.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(img1View.snp.bottom).offset(10)
            make.height.equalTo(20)
        }
        
        img2View.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(des1Label.snp.bottom).offset(40)
            make.size.equalTo(CGSizeMake(262, 210))
        }
        
        img3View.snp.makeConstraints { make in
            make.top.equalTo(img2View.snp.bottom).offset(35)
            make.left.equalTo(kScreenWidth*0.03)
            make.size.equalTo(CGSizeMake((kScreenWidth-kScreenWidth*0.06-16)/2, UIDevice.bk_isiPhone ? 86 : 129))
        }
        
        img4View.snp.makeConstraints { make in
            make.size.centerY.equalTo(img3View)
            make.right.equalTo(-kScreenWidth*0.03)
        }
        
        t1Label.snp.makeConstraints { make in
            make.left.equalTo(kScreenWidth*0.04)
            make.centerY.equalToSuperview().offset(-16)
        }
        
        d1Label.snp.makeConstraints { make in
            make.left.equalTo(t1Label.snp.left)
            make.top.equalTo(t1Label.snp.bottom).offset(8)
        }
        
        t2Label.snp.makeConstraints { make in
            make.left.equalTo(kScreenWidth*0.04)
            make.centerY.equalToSuperview().offset(-16)
        }
        
        d2Label.snp.makeConstraints { make in
            make.left.equalTo(t2Label.snp.left)
            make.top.equalTo(t2Label.snp.bottom).offset(8)
        }
        
        stackView.snp.makeConstraints { make in
            make.top.equalTo(img3View.snp.bottom).offset(23)
            make.centerX.equalToSuperview()
            make.left.right.equalToSuperview().inset(kScreenWidth*0.06)
            make.height.equalTo(115)
        }
        
        img9View.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(stackView.snp.bottom).offset(43)
            make.height.equalTo(30)
            make.width.equalTo(kScreenWidth*0.51)
        }
        
        des2Label.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(img9View.snp.bottom).offset(10)
            make.height.equalTo(18)
        }
        
        personalItemView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(kScreenWidth*0.06)
            make.top.equalTo(des2Label.snp.bottom).offset(30)
            make.height.equalTo(90)
        }
        
        companyItemView.snp.makeConstraints { make in
            make.left.right.height.equalTo(personalItemView)
            make.top.equalTo(personalItemView.snp.bottom).offset(23)
            make.bottom.equalTo(-15)
        }
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.bk_setBackBarButtonItem(isHidden: true)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.bk_setPopSwipe(false)
    }
    
    // MARK: - lazy
    private lazy var scrollView: UIScrollView = {
        let view = UIScrollView()
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        return view
    }()
    
    private lazy var img1View: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_vipservice_p1"))
        iv.contentMode = .scaleAspectFit
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var des1Label = self.bk_addLabel(text: "触手可及的算力加速", font: .systemFont(ofSize: 16, weight: .medium), bgColor: .clear, textColor: .white, align: .center)
    
    private lazy var img2View: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_vipservice_p2"))
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var img3View: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_vipservice_p3"))
        iv.contentMode = .scaleToFill
        iv.clipsToBounds = true
        iv.whenTap { tap in
            
        }
        return iv
    }()
    
    private lazy var img4View: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_vipservice_p4"))
        iv.contentMode = .scaleToFill
        iv.clipsToBounds = true
        iv.whenTap { tap in
            
        }
        return iv
    }()
    
    private lazy var stackView: UIStackView = {
        let _stackView = UIStackView()
        _stackView.axis = .horizontal
        _stackView.spacing = (kScreenWidth-kScreenWidth*0.12-65*4)/3
        _stackView.alignment = .fill
        _stackView.distribution = .fillEqually
        return _stackView
    }()
    
    private lazy var img9View: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_vipservice_p9"))
        iv.contentMode = .scaleAspectFit
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var des2Label = self.bk_addLabel(text: "企业用户支持按需定制本地租用", font: .systemFont(ofSize: 13, weight: .medium), bgColor: .clear, textColor: .white, align: .center)
    
    private lazy var personalItemView: YBVipServiceUserView = {
        let view = YBVipServiceUserView(type: .personal)
        view.whenTap { [unowned self] tap in
            self.callNumber()
        }
        return view
    }()
    
    private lazy var companyItemView: YBVipServiceUserView = {
        let view = YBVipServiceUserView(type: .company)
        view.whenTap { [unowned self] tap in
            self.callNumber()
        }
        return view
    }()
    
}

// MARK: - Private
extension YBVipServiceVC {
    
    private func callNumber() {
        // 不提示

//        UIAlertController.showTwoAlertInRoot(title: "联系客服", msg: "确认拨打：400-1169-868", okTitle: "确定", noStyle: .default) { _ in
//            CommonTool.call("4001169868") { errDes in
//                HintTool.toast(errDes)
//            }
//        }
    }
    
}

// MARK: - YBVipServiceItemView
class YBVipServiceItemView: UIView {
    
    enum ItemType: String {
        case p5
        case p6
        case p7
        case p8
        
        var icon: String {
            return "icon_vipservice_\(self.rawValue)"
        }
        
        var des: String {
            switch self {
            case .p5: return "超多类型\n多样交付"
            case .p6: return "算力加速\n高效办公"
            case .p7: return "灵活便捷\n安全可控"
            case .p8: return "随时随地\n即买即用"
            }
        }
    }
    
    private let itemType: ItemType
    
    init(type: ItemType) {
        itemType = type
        super.init(frame: .zero)
        
        self.addSubviews([imgView, desLabel])
        imgView.snp.makeConstraints { make in
            make.size.equalTo(65)
            make.left.centerX.right.top.equalToSuperview()
        }
        
        desLabel.snp.makeConstraints { make in
            make.centerX.left.bottom.right.equalToSuperview()
            make.top.equalTo(imgView.snp.bottom).offset(10)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var imgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: itemType.icon))
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var desLabel = self.bk_addLabel(text: itemType.des, font: .systemFont(ofSize: 15), bgColor: .clear, textColor: kRGBColor(172, 183, 221), align: .center)
    
}

// MARK: - YBVipServiceUserView
class YBVipServiceUserView: UIView {
    
    enum ItemType: String {
        case personal
        case company
        
        var style: (icon: String, title: String, des: String) {
            switch self {
            case .personal: return ("icon_vipservice_p10", "个人用户", "支持升级云盘和硬盘、显卡升级及更多专属服务支持升级云盘和硬盘、显卡升级及更多专属服务")
            case .company: return ("icon_vipservice_p11", "企业用户", "支持整机交付，企业一致性购买账号，还可根 据具体需求场景提供特定功能定制，提供全套解决方案")
            }
        }
    }
    
    private let itemType: ItemType
    
    init(type: ItemType) {
        itemType = type
        super.init(frame: .zero)
        
        self.addSubview(bgView)
        bgView.addSubviews([imgView, titleLabel, desLabel])
        
        bgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        imgView.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(45, 40))
            make.centerY.equalToSuperview()
            make.left.equalTo(kScreenWidth*0.045)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(18)
            make.centerY.equalTo(imgView.snp.top)
        }
        
        desLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.left)
            make.right.equalTo(-25)
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
            make.bottom.equalTo(-20)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var bgView: UIView = {
        let view = UIView(color: .white)
        view.bk_addStyleWith(cornerRadius: 10, corners: .allCorners)
        return view
    }()
    
    private lazy var imgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: itemType.style.icon))
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var titleLabel = self.bk_addLabel(text: itemType.style.title, font: .systemFont(ofSize: 20, weight: .bold), bgColor: .clear, textColor: kRGBColor(29, 38, 71))
    
    private lazy var desLabel = self.bk_addLabel(text: itemType.style.des, font: .systemFont(ofSize: 14), bgColor: .clear, textColor: kRGBColor(148, 152, 163))
    
}
